package com.zpp.wedding_admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpp.wedding_admin.entity.Camerist;
import com.zpp.wedding_admin.entity.User;
import com.zpp.wedding_admin.entity.vo.IDs;
import com.zpp.wedding_admin.service.IUserService;
import com.zpp.wedding_common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 朱盼盼
 * @since 2022-12-29
 */
@Api(description = "用户操作")
@CrossOrigin
@RestController
@RequestMapping("/weddingadmin/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "分页获取所有用户")
    @GetMapping("getUserPageVo/{currentPage}/{size}")
    public R getUserPageVo(@PathVariable Long currentPage,
                             @PathVariable Long size) {
        Page<User> page = new Page<User>(currentPage, size);
        userService.page(page, null);
        List<User> userList = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("userList", userList).data("total", total);
    }

    @ApiOperation(value = "通过id获取用户信息")
    @GetMapping("getUserById/{id}")
    public R getUserById(@PathVariable String id) {
        User user = userService.getById(id);
        if ( user != null ) return R.ok().data("user", user);
        return R.error();
    }

    @ApiOperation(value = "封禁用户")
    @PostMapping("forbidUser/{id}")
    public R forbidUser(@PathVariable String id) {
        boolean b = userService.setUserForbid(id);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "解封用户")
    @PostMapping("releaseUser/{id}")
    public R releaseUser(@PathVariable String id) {
        boolean b = userService.setUserRelease(id);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping("/deleteAllSelect")
    public R deleteAllSelect(@RequestBody Integer Ids[]) {
        boolean b = userService.deleteAllSelect(Ids);
        if ( b ) return R.ok();
        return R.error();
    }

    @ApiOperation(value = "根据id删除一个")
    @DeleteMapping("/deleteOneById/{id}")
    public R deleteOneById(@PathVariable Integer id) {
        boolean b = userService.removeById(id);
        if ( b ) return R.ok();
        return R.error();
    }
}
